//
//  SSNormalDetailHeader.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/6/1.
//

import UIKit

let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height

class SSNormalDetailHeader: UIView {

    @IBOutlet weak var titleLab1: UILabel!
    
//    @IBOutlet weak var tagLab: UILabel!
    
    @IBOutlet weak var priceLab: UILabel!
    
    @IBOutlet weak var specsDesLab: UILabel!
    
    @IBOutlet weak var tagBGView: UIView!
    
    @IBOutlet weak var cycleView: UIView!
    var cycleView1 = ZCycleView()
    
    @IBOutlet weak var couponTitle: UILabel!
    
    let images = [UIImage(named: "p700-300-1"),
                  UIImage(named: "p700-300-2"),
                  UIImage(named: "p700-300-3"),
                  UIImage(named: "p700-300-4"),
                  UIImage(named: "p700-300-5")]
    
    class func headerView()->SSNormalDetailHeader{
        return Bundle.main.loadNibNamed("SSNormalDetailHeader", owner: nil, options: nil)?.last as! SSNormalDetailHeader
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let cycleView1 = ZCycleView(frame: CGRect(x: 0, y: 0, width:width, height: 340))
//        cycleView1.placeholderImage = #imageLiteral(resourceName: "placeholder")
        cycleView1.placeholderImage = UIImage(named: "banner_place_holder")
        cycleView1.scrollDirection = .horizontal
        cycleView1.delegate = self
//        cycleView1.reloadItemsCount(images.count)
//        cycleView1.itemZoomScale = 1.2
//        cycleView1.itemSpacing = 10
        cycleView1.initialIndex = 0
        cycleView1.isAutomatic = true
//        cycleView1.isInfinite = false
        cycleView1.isInfinite = true
        cycleView1.itemSize = CGSize(width: width, height: 310)
        self.cycleView1 = cycleView1
        cycleView.addSubview(cycleView1)
//        cycleView1.frame = CGRect(x: 0, y: 0, width:width, height: 340)
        
    }
    
    var normal:SSNormal = SSNormal(){
        didSet{
            
            cycleView1.reloadItemsCount(normal.listSwiperImg.count)
            
            titleLab1.text = normal.pkgName
            
            couponTitle.text = normal.couponText
            
            var price:Float = 0
            
            for item in normal.listSpecs{
                if item.isSelected {
                    price = item.specsPrice
                }
            }
            
            for v in tagBGView.subviews{
                v.removeFromSuperview()
            }
            
            var lastLab = UILabel()
            
            for (index,item) in normal.listTag.enumerated(){
                let lab = UILabel()
                lab.layer.cornerRadius = 4
                lab.layer.masksToBounds = true
                lab.textColor = UIColor(hex: "#197FD6")
                lab.text = item
                lab.textAlignment = .center
                lab.font = UIFont.systemFont(ofSize: 12)
                lab.backgroundColor = UIColor(hex: "E0EDF9")
                var width = item.sizeWithText(font: lab.font, size: CGSize(width: CGFloat.zero, height: 20)).width
                width = width + 16
                lab.frame = CGRect(x: lastLab.frame.maxX + 4, y: 0, width: width, height: 20)
                if(index == 0){
                    lab.frame = CGRect(x: 0, y: 0, width: width, height: 20)
                }
                tagBGView.addSubview(lab)
                lastLab = lab
            }
            
            refreshSpecs()
            
        }
    }
    
    //刷新价格、套餐
    func refreshSpecs() {
        for item in self.normal.listSpecs{
            if(item.isSelected){
                self.specsDesLab.text = item.specsDescribe
                self.priceLab.text = "$\(item.specsPrice)"
                
//                let attributedString2 = NSMutableAttributedString(string:"券后价:$\(item.specsPrice)")
//                attributedString2.addAttribute(NSAttributedString.Key.font, value: UIFont.boldSystemFont(ofSize: 16), range: NSRange(location: 0, length: 4))
//                self.priceLab.attributedText = attributedString2
            }
        }
    }

    @IBAction func specsAction(_ sender: Any) {
        let vc = SSNormalTimeViewController()
        vc.normal = normal
        vc.refresh {
            self.refreshSpecs()
        }
        UIViewController.current().present(vc, animated: true)
//        UIViewController.current().navigationController?.pushViewController(vc, animated: true)
        
    }
    
    @IBAction func couponAction(_ sender: Any) {
        let vc = SSGetCouponViewController()
        UIViewController.current().present(vc, animated: true)

    }
}


extension SSNormalDetailHeader: ZCycleViewProtocol {
    func cycleViewRegisterCellClasses() -> [String: AnyClass] {
        return ["CustomCollectionViewCell": CustomCollectionViewCell.self]
    }

    func cycleViewConfigureCell(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, realIndex: Int) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCollectionViewCell", for: indexPath) as! CustomCollectionViewCell
//        cell.imageView.image = images[realIndex]
        if(self.normal.listSwiperImg.count > 0){
            print("---\(indexPath.row),\(indexPath.section)")
            cell.imageView.setImage(with: self.normal.listSwiperImg[realIndex].imgUrl, placeholderImage: UIImage(named: ""))
        }
        
        return cell
    }
    
    func cycleViewDidScrollToIndex(_ cycleView: ZCycleView, index: Int) {
        
    }
    
    func cycleViewConfigurePageControl(_ cycleView: ZCycleView, pageControl: ZPageControl) {
        pageControl.isHidden = false
        pageControl.currentPageIndicatorTintColor = UIColor(hex: "2474FC")!
        pageControl.pageIndicatorTintColor = UIColor(hex: "D0DEEB")!
        pageControl.frame = CGRect(x: 0, y: cycleView.bounds.height-25, width: cycleView.bounds.width, height: 25)
    }
}
